﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BACKOFFICE
{
    class DataTableClass
    {
        public static DataTable Receipt = new DataTable("Receipt");
        public static DataTable ReceiptData = new DataTable("ReceiptData");
        public static DataTable MenuItems = new DataTable("MenuItems");
        public static DataTable MenuItemsCategory = new DataTable("MenuItemsCategory");
        public static DataTable EmployeeLogin = new DataTable("EmployeeLogin");
        public static DataTable EmployeeInfo = new DataTable("EmployeeInfo");
        public static DataTable EmployeeTimeSheet = new DataTable("EmployeeTimeSheet");
        public static DataTable EmployeeTips = new DataTable("EmployeeTips");
        public static DataTable EmployeeSchedule = new DataTable("EmployeeSchedule");
        public static DataTable PaymentTypes = new DataTable("PaymentTypes");
        public static DataTable JobTitles = new DataTable("JobTitles");
        public static DataTable ModificationList = new DataTable("ModificationList");
        public static DataTable CCPaymentInfo = new DataTable("CCPaymentInfo");

        public static Dictionary<string, double> dictionaryPrices = new Dictionary<string, double>();
        public static Dictionary<string, string> dictionaryCategorys = new Dictionary<string, string>();
        public static List<string> categoryList = new List<string>();
        public static List<string> DataFilesList = new List<string>();

        public static DataSet POS = new DataSet();

        public static bool IsServerConnected()
        {
            using (SqlConnection connection = new SqlConnection(StaticProperties.myConnectionString))
            {
                try
                {
                    connection.Open();
                    connection.Close();
                    return true;
                }
                catch (SqlException)
                {
                    return false;
                }

            }
        }

        public static void LoadDataTable(string Table, DataTable DataTable)
        {
            SplashScreen1._main.labelControl2.Text = "LOADING " + Table.ToUpper() + " DATATABLE";
            SqlConnection conn = new SqlConnection(StaticProperties.myConnectionString);
            conn.Open();

            string query = "SELECT * FROM " + Table;

            SqlCommand cmd = new SqlCommand(query, conn);
            using (SqlDataAdapter a = new SqlDataAdapter(cmd))
            {

                a.Fill(DataTable);
            }

            if (!POS.Tables.Contains(Table))
            {
                POS.Tables.Add(Table);
            }

            conn.Close();

        }

        public static void LoadMenuItemsDictionary()
        {
            try
            {
                foreach (DataRow item in MenuItems.Rows)
                {

                    if (!dictionaryCategorys.Keys.Contains(item.Field<string>(2).Trim()))
                    {
                        dictionaryCategorys.Add(item.Field<string>(2).Trim(), item.Field<string>(1).Trim());
                        dictionaryPrices.Add(item.Field<string>(2).Trim(), Convert.ToDouble(item.Field<decimal>(4)));
                    }
                }
            }
            catch (Exception E)
            {
                MessageBox.Show(E.ToString());
            }
        }

        public static void LoadMenuCategoryDictionary()
        {
            try
            {
                foreach (DataRow item in MenuItemsCategory.Rows)
                {
                    if (!categoryList.Contains(item.Field<string>(1).Trim()))
                    {
                        categoryList.Add(item.Field<string>(1).Trim());
                    }
                }
            }
            catch (Exception E)
            {
                MessageBox.Show(E.ToString());
            }
        }

        public static void SendDataToSqlServer(string TableName)
        {
            //DataSet POS2 = new DataSet("POS2");
            POS.Tables.Clear();
            POS.Tables.Add(Receipt);
            POS.Tables.Add(EmployeeInfo);
            POS.Tables.Add(ReceiptData);
            POS.Tables.Add(MenuItems);
            POS.Tables.Add(MenuItemsCategory);
            POS.Tables.Add(EmployeeLogin);
            POS.Tables.Add(EmployeeTimeSheet);
            POS.Tables.Add(EmployeeTips);
            POS.Tables.Add(EmployeeSchedule);
            POS.Tables.Add(PaymentTypes);
            POS.Tables.Add(JobTitles);
            POS.Tables.Add(ModificationList);


            SqlConnection cn = new SqlConnection();
            DataSet CustomersDataSet = new DataSet();
            SqlDataAdapter da;
            SqlCommandBuilder cmdBuilder;

            cn.ConnectionString = StaticProperties.myConnectionString;
            try
            {
                cn.Open();
                da = new SqlDataAdapter("select * from " + TableName, cn);
                cmdBuilder = new SqlCommandBuilder(da);

                //MessageBox.Show(POS.Tables[TableName].Rows.Count.ToString());
                da.Update(POS, TableName);
                cn.Close();
            }
            catch (SqlException E)
            {
                MessageBox.Show(E.Message);
            }
        }

        public static void SaveDataSetToFile(DataTable SelectedDataTable)
        {

            try
            {
                SelectedDataTable.WriteXml(SelectedDataTable.TableName + ".xml");


            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }


        }
    }
}
